﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using DynamicQueryLib;
using LinqKit;

namespace AuditModel
{
    public class ScoreBL:IScoreBL
    {
        AuditDBEntities db;
        public ScoreBL()
        {
            db = new AuditDBEntities();
        }
        #region IScoreBL Members

        public ObjectResult<sp_GetArchiveScores_Result> GetArchiveScores(string archiveID)
        {
            return db.sp_GetArchiveScores(archiveID);
        }

        #endregion

        #region IScoreBL Members


        public ObjectResult<sp_GetEventSubjectScores_Result> GetEventSubjectScores(string annualEventID, int subjectID)
        {
            return db.sp_GetEventSubjectScores(annualEventID, subjectID);
        }

        #endregion

        #region IScoreBL Members


        public List<Score> GetExpertScores(string expertSerialNumber)
        {
            var r = (from s in db.Scores
                     where s.ExpertJudgeSerialNumber == expertSerialNumber
                     group s by s.ArchiveID into r1
                     select r1.OrderByDescending(t => t.SubmitTime)
                     .FirstOrDefault())
                     .ToList();             
            return r;
        }

        public Score GetExpertArchiveScore(string expertSerialNumber, string archiveID)
        {
            var r = ((from s in db.Scores
                      where s.ExpertJudgeSerialNumber == expertSerialNumber && s.ArchiveID == archiveID
                      select s).OrderByDescending(t => t.SubmitTime).FirstOrDefault());

            return r;
                  
        }
        #endregion

        #region IScoreBL Members


        public void DeleteScores(string expertSerialNumber, string archiveID)
        {
            var scores = db.Scores.Where(s => s.ExpertJudgeSerialNumber == expertSerialNumber && s.ArchiveID == archiveID).ToList();
            foreach (var s in scores)
            {
                db.DeleteObject(s);
            }
            db.SaveChanges();
        }

        #endregion

        #region IScoreBL Members


        public List<Score> GetScores(string annualEvnetID,int subjectID, string expertSerialNumber, string archiveID)
        {
            List<Filter> filterList = new List<Filter>();
            if (expertSerialNumber.Trim()!="")
            {
                Filter f = new Filter();
                f.Connector = FilterConnectorType.AND;
                f.PropertyName = "ExpertJudgeSerialNumber";
                f.PropertyType = typeof(string);
                f.Values.Add(expertSerialNumber);
                f.Operator = RelationalOperators.Contains;
                filterList.Add(f);
            }
            if (archiveID.Trim() != "")
            {
                Filter f = new Filter();
                f.Connector = FilterConnectorType.AND;
                f.PropertyName = "ArchiveID";
                f.PropertyType = typeof(string);
                f.Values.Add(archiveID);
                f.Operator = RelationalOperators.Contains;
                filterList.Add(f);
            }
            var predicate = LambdaExpressionHelper<Score>.CreatePredicate<Score>(filterList);

            var r = from s in db.Scores
                    join a in db.Archives on s.ArchiveID equals a.ID
                    where a.AnnualEventID==annualEvnetID
                    select s;

            if (subjectID > 0)
            {
                r = from t in r
                    join a in db.Archives on t.ArchiveID equals a.ID
                    where a.SubjectID == subjectID
                    select t;
            }

            return r.AsExpandable().Where(predicate).ToList();
        }

        #endregion
    }
}
